package com.cmcc.linlaker.ui.mine;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.adapter.BookPageAdapter;
import com.cmcc.linlaker.ui.mine.bean.BookTypeBean;
import com.cmcc.linlaker.ui.mine.bean.MessageBean;
import com.cmcc.linlaker.ui.mine.fragment.BookFragment;
import com.cmcc.linlaker.ui.query.adapter.QueryPageAdapter;
import com.cmcc.linlaker.ui.query.fragment.FarmerFragment;
import com.cmcc.linlaker.ui.query.fragment.ProjectFragment;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);

        BarUtils.initStatusBar(this);

        initView();
    }

    private TabLayout indicator;
    private ViewPager mViewPager;
    private BookPageAdapter pagerAdapter;

    private void initView() {

        mViewPager = findViewById(R.id.view_pager);

        indicator = findViewById(R.id.indicator);

        getBookTypeList();
    }

    private List<Fragment> fragmentList = new ArrayList<>();

    public void back(View view) {
        finish();
    }

    /**
     * 知识库分类
     */
    private void getBookTypeList() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();

        helper.sendPOSTRequest(ApiDefine.BOOK_TYPE_LIST, BookTypeBean.class, params, true, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    BookTypeBean bean = (BookTypeBean) data;

                    if (bean.code.equals("0")) {
                        List<BookTypeBean.BookTypeListBean> list = bean.rows;

                        for (int i = 0; i < list.size(); i++) {
                            BookTypeBean.BookTypeListBean typeListBean = list.get(i);
                            BookFragment fragment = BookFragment.newInstance(typeListBean);
                            fragmentList.add(fragment);
                        }

                        pagerAdapter = new BookPageAdapter(getSupportFragmentManager(), list, fragmentList);

                        mViewPager.setOffscreenPageLimit(2);
                        mViewPager.setAdapter(pagerAdapter);
                        indicator.setTabMode(TabLayout.MODE_FIXED);
                        indicator.setupWithViewPager(mViewPager);
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(BookActivity.this);
                    }
                }
            }
        });
    }

}
